<template>
  <el-dialog :visible="showDialog" title="新增员工" width="40%" @close="closeFn">
    <!-- 表单 -->
    <el-form ref="form" :model="formData" :rules="rules" label-width="120px">
      <el-form-item label="姓名" prop="username">
        <el-input v-model="formData.username" style="width:75%" placeholder="请输入姓名" />
      </el-form-item>
      <el-form-item label="手机" prop="mobile">
        <el-input v-model="formData.mobile" style="width:75%" placeholder="请输入手机号" />
      </el-form-item>
      <el-form-item label="入职时间" prop="timeOfEntry">
        <el-date-picker v-model="formData.timeOfEntry" style="width:75%" placeholder="请选择入职时间" />
      </el-form-item>
      <el-form-item label="聘用形式" prop="formOfEmployment">
        <el-select v-model="formData.formOfEmployment" style="width:75%" placeholder="请选择">
          <!-- 🔔渲染选择: 聘用形式 -->
          <el-option v-for="item in hireType" :key="item.id" :label="item.value" :value="item.id" />
        </el-select>
      </el-form-item>
      <el-form-item label="工号" prop="workNumber">
        <el-input v-model="formData.workNumber" style="width:75%" placeholder="请输入工号" />
      </el-form-item>
      <el-form-item label="部门" prop="departmentName">
        <el-input v-model="formData.departmentName" style="width:75%" placeholder="请选择部门" @focus="isShow=true" />
        <!-- 🔔渲染部门列表
        data 数据 props 配置  default-expand-all 默认选择
         -->
        <el-tree
          v-show="isShow"
          :data="departmentList"
          :props="{label:'name'}"
          default-expand-all
          @node-click="seleteDepartment"
        />
      </el-form-item>
      <el-form-item label="转正时间" prop="correctionTime">
        <el-date-picker v-model="formData.correctionTime" style="width:75%" placeholder="请选择转正时间" />
      </el-form-item>
    </el-form>
    <!-- 插槽: 确认/取消 -->
    <!-- footer插槽 -->
    <template v-slot:footer>
      <el-row type="flex" justify="center">
        <el-col :span="6">
          <el-button size="small" @click="closeFn">取消</el-button>
          <el-button type="primary" size="small" @click="confirmFn">确定</el-button>
        </el-col>
      </el-row>
    </template>
  </el-dialog>
</template>

<script>
/* 渲染下拉 */
import employess from '@/api/constant/employees'
import { getComponents } from '@/api/components'
import { listToTree } from '@/utils/index'
import { addEmployee } from '@/api/employees'
export default {
  props: {
    // 控制是否显示
    showDialog: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      // 编辑表单数据
      formData: {
        username: '',
        mobile: '',
        formOfEmployment: '', // 聘用形式
        workNumber: '', // 工号
        departmentName: '', // 部门
        timeOfEntry: '', // 入职时间
        correctionTime: '' // 转正时间
      },
      // 校验规则
      rules: {
        username: [
          { required: true, message: '用户名不能为空！', trigger: 'blur' },
          { min: 1, max: 10, message: '用户名长度1到10为', trigger: 'blur' }
        ],
        mobile: [
          { required: true, message: '手机号不能为空！', trigger: 'blur' },
          { pattern: /^1[3-9]\d{9}$/, message: '手机号格式不正确', trigger: 'blur' }
        ],
        formOfEmployment: [
          { required: true, message: '聘用形式不能为空！', trigger: 'blur' }
        ],
        workNumber: [
          { required: true, message: '工号不能为空！', trigger: 'blur' }
        ],
        departmentName: [
          { required: true, message: '部门名称不能为空！', trigger: 'chang' }
        ],
        timeOfEntry: [
          { required: true, message: '入职时间不能为空！', trigger: 'blur' }
        ]
      },
      // 渲染聘用形式（下拉）
      hireType: employess.hireType,
      // 渲染部门列表（下拉）
      departmentList: [],
      // 控制下拉部门列表显示
      isShow: false
    }
  },
  async created() {
    // 1.导入getComponents，调用获取部门列表信息
    const res = await getComponents()
    // 2. 导入树形结构转换函数 生成树形结构
    this.departmentList = listToTree(res.depts)
  },
  methods: {
    /* 点击新增提交 */
    async confirmFn() {
      // 1.表单校验
      await this.$refs.form.validate()
      // 2.调用接口提交表单
      await addEmployee(this.formData)
      // 3.处理关闭逻辑
      this.closeFn()
      // 4. 提交父组件重新渲染数据
      this.$emit('updateEvent')
    },
    /* 部门选择后关闭选择 */
    seleteDepartment(data) {
      // 1.将选择的值赋给输入框
      this.formData.departmentName = data.name
      // 2.关闭选择
      this.isShow = false
    },
    /* 处理新增窗口关闭 */
    closeFn() {
      // 1.清空表单
      this.formData = {
        username: '',
        mobile: '',
        formOfEmployment: '', // 聘用形式
        workNumber: '', // 工号
        departmentName: '', // 部门
        timeOfEntry: '', // 入职时间
        correctionTime: '' // 转正时间
      }
      // 2. 重置校验
      this.$refs.form.resetFields()
      // 3. 关闭窗口
      this.$emit('update:showDialog', false)
    }
  }
}
</script>
<style lang="scss" scoped>
.el-form-item{
  position: relative;
  .el-tree{
    height: 150px;
    width: 75%;
    position: absolute;
    z-index: 1;
    overflow-y: auto;
  }
}
</style>
<style>
::-webkit-scrollbar-track-piece {
  border-radius: 0
}
::-webkit-scrollbar {
  width: 5px;
  height: 10px
}
::-webkit-scrollbar-thumb {
  height: 50px;
  background-color: #b8b8b8;
  border-radius: 6px;
  outline-offset: -2px;
  filter: alpha(opacity = 50);
  -moz-opacity: 0.5;
  -khtml-opacity: 0.5;
  opacity: 0.5
}
::-webkit-scrollbar-thumb:hover {
  height: 50px;
  background-color: #878987;
  border-radius: 6px
}
</style>
